<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="邮件名称" prop="project">
        <el-input v-model="queryParams.project" placeholder="请输入邮件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收件人" prop="recipientsName">
        <el-input v-model="queryParams.recipientsName" placeholder="请输入收件人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" filterable>
          <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="outboxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="邮件名称" align="center" prop="project" />
      <el-table-column label="收件人" align="center" prop="recipientsName" />
      <el-table-column label="所属部门" align="center" prop="deptName" />

      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">普通文件</span>
          <span v-else>机密文件</span>
        </template>
      </el-table-column>

      <el-table-column label="发件时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleSearch(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['email:outbox:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getOutBoxList" />

    <!-- 添加或修改邮箱发件箱对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" append-to-body>
      <el-form :model="formData" :rules="rules" size="medium" label-width="80px">

        <el-form-item label="发件人" prop="recipientsName">
          <el-input v-model="formData.recipientsName" placeholder="请输入发件人" clearable :style="{ width: '100%' }"
            disabled>
          </el-input>
        </el-form-item>

        <el-form-item label="主题" prop="project">
          <el-input v-model="formData.project" placeholder="请输入主题" clearable :style="{ width: '100%' }" disabled>
          </el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择类型" clearable filterable :style="{ width: '100%' }"
            disabled>
            <el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="附件" prop="address">
          <file-upload v-model="formData.address" disable="true" />
        </el-form-item>

        <el-form-item label="正文" prop="content">
          <el-input v-model="formData.content" type="textarea" placeholder="请输入" :maxlength="255"
            :autosize="{ minRows: 8, maxRows: 8 }" :style="{ width: '100%' }" disabled></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectOutBoxList, selectByCondtionList, getOutbox, updateSymbol } from "@/api/email/outbox";

export default {
  name: "Outbox",
  dicts: ['email_table_status', 'email_table_type'],
  data() {
    return {
      initialQueryParams: {
        pageNum: 1,
        pageSize: 10,
        project: null,
        recipientsName: null,
        type: '',
      },
      // 嵌套表单属性  
      dialogTableVisible: false,
      dialogFormVisible: false,
      // 回显表单内容 formData  
      formData: {
        recipientsName: '',
        project: undefined,
        type: '',
        content: undefined,
        deptId: '',
        address: null,
      },
      // 表单重置
      reset() {
        this.formData = {
          id: null,
          project: null,
          content: null,
          createId: null,
          createBy: null,
          recipientsId: null,
          deptId: null,
          deptName: null,
          type: null,
          status: null,
          address: null,
          createTime: null,
          updateTime: null,
          symbol: null,
          garbageDelete: null,
          delFlag: null
        };
        this.resetForm("formData");
      },
      // 邮件类型选择  
      typeOptions: [
        { value: 0, label: '普通文件' },
        { value: 1, label: '机密文件' },
      ],
      // 遮罩层  
      loading: true,
      // 选中数组  
      ids: [],
      // 非单个禁用  
      single: true,
      // 非多个禁用  
      multiple: true,
      // 显示搜索条件  
      showSearch: true,
      // 总条数  
      total: 0,
      // 邮箱发件箱表格数据  
      // 表格数据  
      outboxList: [
        {
          // id: '',
          project: '',
          recipientsName: '',
          deptName: '',
          type: '',
          createTime: ''
        }
      ],
      // 弹出层标题  
      title: "",
      // 是否显示弹出层  
      open: false,
      // 查询参数  
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        project: null,
        recipientsName: null,
        type: '',
      },
      // 表单参数  
      form: {},
      // 表单校验  
      rules: {
      }
    };
  },
  activated() {
    this.queryParams = { ...this.initialQueryParams };
    this.getOutBoxList();
  },
  created() {
    this.queryParams = { ...this.initialQueryParams };
    this.getOutBoxList();
  },


  methods: {
    getOutBoxList() {
      getOutbox(id).then((response) => {
        this.formData = response.data;
        this.title = "页面切换重置";
      });
    },
    initialize() {
      // 初始化代码，如加载数据等  
      this.queryParams = { ...this.initialQueryParams };
      this.getOutBoxList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = { ...this.initialQueryParams };
      this.getOutBoxList();
    },
    getRest() {
      this.getOutBoxList();
    },

    // 查看 回显表单  getOutbox
    handleSearch(row) {
      const id = row.id;
      getOutbox(id).then((response) => {
        this.formData = response.data;
        this.title = "获取回显邮件内容";
      });
      this.dialogFormVisible = true;
    },

    /** 查询当前用户邮箱发件箱列表 */
    getOutBoxList() {
      this.loading = true;
      selectOutBoxList(this.queryParams).then(response => {
        this.outboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 根据条件查询当前用户邮箱发件箱列表 */
    getOutBoxByCondtionList() {
      this.loading = true;
      selectByCondtionList(this.queryParams).then(response => {
        this.outboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getOutBoxByCondtionList();
    },

    // 多选框选中数据  
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除邮箱发件箱编号为"' + ids + '"的数据项？').then(function () {
        return updateSymbol(ids);
      }).then(() => {
        this.getOutBoxList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};  
</script>